<?php


namespace app\common\repositories\system\merchant;


use app\common\dao\system\merchant\ArticleDao;
use app\common\model\article\ArticleContent;
use app\common\repositories\BaseRepository;
use think\facade\Db;

class ArticleRepository extends BaseRepository
{
    public function __construct(ArticleDao $dao)
    {
        $this->dao = $dao;
    }

    public function getFormatList($merId = 0)
    {
        return $this->dao->getAll($merId)->toArray();
    }

    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function search( array $where, $page, $limit)
    {
        $query = $this->dao->search($where)->order('create_time DESC');
        $count = $query->count($this->dao->getPk());
        $list = $query->page($page, $limit)->hidden(['update_time'])->select();
        return compact('count', 'list');
    }

    /**
     * @param int $id
     * @param $except
     * @return bool
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function merExists(int $id, $except = null)
    {
        return $this->dao->merFieldExists($this->dao->getPk(), $id, $except);
    }


    public function ApiExists(int $id)
    {
        return $this->dao->getWhere([$this->dao->getPk() => $id,'status' => 1]);
    }

    public function getApiList(array $where, $page, $limit, $orderBy = null ,$field ='*')
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $query = $query->page($page, $limit)->setOption('field', [])->field($field);
        if($orderBy){
            $query->order($orderBy);
        }
        $list = $query->select();
        return compact('count', 'list');
    }

}
